/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plc;

import MyPackage.AllObjs;
import MyPackage.Button;
import MyPackage.Device;
import MyPackage.MyTimer;
import java.io.StringReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import java_cup.runtime.Symbol;
import plc.language.Lexer;
import plc.language.parser;

/**
 *
 * @author PhatChai
 */
public class PLCController {

    private PLCEditor editor;
    private PLCEmulator emulator;
    private AllObjs all;

    public void setEditor(PLCEditor editor) {
        this.editor = editor;
    }

    public void parse(String language) {
        try {
            // remove old objects.
            all = null;

            parser p = new parser(new Lexer(new StringReader(language)));

            // get all objects (Button ,Command, Device,...)

            Symbol s = p.parse();

            // There are errors
            if (p.isIsError()) {

                int line = p.getErrorLine();
                editor.output(p.getErrorMessage());
//                editor.setErrorLine(line);
            } else {

                all = (AllObjs) s.value;
                if (all.error) {

                    int line = p.getErrorLine();
                    for (String e : all.error_list) {
                        editor.output(e);
                    }
//                    editor.setErrorLine(line);
                } else {
                    editor.output("Passed.");
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(PLCEditor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void run() {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                editor.output("------------------ Creating emulator ------------------");
                // display emulator
                emulator = new PLCEmulator(PLCController.this, all.device_list, all.func_list);

                createButtons();

                createDevices();

                createSchedules();

                emulator.setWattHour(all.elec_cost);

                emulator.createSummryUpdateThread(all.elec_cost);

                emulator.setVisible(true);

                emulator.output("Run command:");
                emulator.doCommands(all.cmd_list);

            }
        });
    }

    /**
     * create buttons from button list
     */
    private void createButtons() {
        int i = 1;
        for (Button b : all.button_list) {
            System.out.println(i + " create button label: " + b.label);
            editor.output(i + " create button label: " + b.label);
            emulator.createButton(b);
            i++;
        }
    }

    private void createDevices() {
        int numDevice = all.device_list.size();
        System.out.println("Device size: " + numDevice);
        int i = 0;
        emulator.initDevicePane(numDevice);
        for (Device d : all.device_list) {
            System.out.println((i + 1) + " create Device : " + d.name);
            editor.output((i + 1) + " create Device : " + d.name);
            emulator.createDevice(i, d);
            i++;
        }
    }

    private void createSchedules() {
        if (all.timer_list != null) {
            int numDevice = all.timer_list.size();
            System.out.println("Timer size: " + numDevice);
            int i = 0;
            for (MyTimer t : all.timer_list) {
                System.out.println((i + 1) + " create Timer : " + t.timeStr);
                editor.output((i + 1) + " create Timer : " + t.timeStr);
                emulator.createSchedule(t);
                i++;
            }
        }
    }
}
